import Taro from '@tarojs/taro';
import u from 'updeep'
import request from '../util/request'

export default {
    namespace: 'personalCenter',
    state: {
        listTab: [{
            name: '我的企业',
            photo: 'wdqy',
            router:'/pages/companyRelease/index',
        },{
            name: '我的发布',
            photo: 'wdfb'
        },{
            name: '我的订阅',
            photo: 'wddy'
        }],
        userInfo:{

        }
    },
    reducers: {
		updateState(state, {payload}) {
			return u({
				...payload
			},state)
		}
    },
    effects: {
      * getUserInfo({payload = {}},{select,put,call}){

        const {data} = yield call(request, {
          url: SERVERURL + 'user/info',
          method: 'get'
        })

        if (data && data === "unauthc") {
          Taro.navigateTo({
            url: `/pages/login/index`
          })
          Taro.showToast({
            title: ("会话过时"),
            icon: 'none'
          })
        }

        if (data.code==='000000'){
          yield put({
            type: 'updateState',
            payload: {
              userInfo: data.data
            }
          })
        }
      },

      * updateUserInfo({payload = {}},{select,put,call}){
        const {
          userInfo
        } = yield select(({personalCenter}) => personalCenter)


        const {data} = yield call(request, {
          url: SERVERURL + 'user/update',
          method: 'post',
          data:{
            username:userInfo.username,
            avatarUrl:userInfo.avatarUrl
          }
        })

        if (data && data === "unauthc") {
          Taro.navigateTo({
            url: `/pages/login/index`
          })
          Taro.showToast({
            title: ("会话过时"),
            icon: 'none'
          })
        }

        if ( !data || data.code!=='000000'){
          Taro.showToast({
            title: (data || {}).msg || '操作失败',
            icon: 'none'
          })
        }

      }
    }
};
